package com.ywk.base.dataStructure.binarytree;

//数组的顺序存储二叉树
public class ArrayBinaryTreeDemo {

	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
		ArrayBinaryTree arrBinaryTree=new ArrayBinaryTree(arr);
		//arrBinaryTree.preOrder(0);
		arrBinaryTree.infixOrder(0);
		arrBinaryTree.postOrder(0);
	}
}

class ArrayBinaryTree {
	private int[] arr;

	public ArrayBinaryTree(int[] arr) {
		super();
		this.arr = arr;
	}
	
	//前序遍历
	public void preOrder(int index) {
		System.out.println(arr[index]);
		if (2 * index + 1 < arr.length) {
			preOrder(2 * index + 1);
		}
		if (2 * index + 2 < arr.length) {
			preOrder(2 * index + 2);
		}
	}
	
	//中序遍历
	public void infixOrder(int index) {
		if(2*index+1<arr.length) {
			infixOrder(2*index+1);
		}
		System.out.println(arr[index]);
		if(2*index+2<arr.length) {
			infixOrder(2*index+2);
		}
	}
	
	//后序遍历
	public void postOrder(int index) {
		if(2*index+1<arr.length) {
			postOrder(2*index+1);
		}
		if(2*index+2<arr.length) {
			postOrder(2*index+2);
		}
		System.out.println(arr[index]);
	}
}